import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GameMain extends JFrame {
    private GamePanel gamePanel;
    JButton startButton;
    JButton pauseButton;
    public JButton resetButton;
    private JLabel scoreLabel;
    private int score = 0;

    public GameMain() {
        setTitle("太空射击游戏");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);

        // 初始化游戏面板
        gamePanel = new GamePanel(this);

        // 初始化控制按钮
        startButton = new JButton("开始游戏");
        pauseButton = new JButton("暂停游戏");
        resetButton = new JButton("重置游戏");
        scoreLabel = new JLabel("分数: 0");

        // 设置按钮样式
        Font buttonFont = new Font("Arial", Font.BOLD, 14);
        startButton.setFont(buttonFont);
        pauseButton.setFont(buttonFont);
        resetButton.setFont(buttonFont);
        scoreLabel.setFont(new Font("Arial", Font.BOLD, 16));

        // 设置按钮背景色和前景色
        startButton.setBackground(new Color(50, 205, 50)); // 绿色
        startButton.setForeground(Color.WHITE);
        pauseButton.setBackground(new Color(255, 165, 0)); // 橙色
        pauseButton.setForeground(Color.WHITE);
        resetButton.setBackground(new Color(220, 20, 60)); // 红色
        resetButton.setForeground(Color.WHITE);

        // 设置按钮事件
        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gamePanel.startGame();
                startButton.setEnabled(false);
                pauseButton.setEnabled(true);
                resetButton.setEnabled(true);
            }
        });

        pauseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gamePanel.pauseGame();
                if (gamePanel.isPaused()) {
                    pauseButton.setText("继续游戏");
                } else {
                    pauseButton.setText("暂停游戏");
                }
            }
        });

        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gamePanel.resetGame();
                updateScore(0);
                startButton.setEnabled(true);
                pauseButton.setText("暂停游戏");
                pauseButton.setEnabled(false);
                resetButton.setEnabled(false);
            }
        });

        // 初始状态下暂停和重置按钮不可用
        pauseButton.setEnabled(false);
        resetButton.setEnabled(false);

        // 创建控制面板
        JPanel controlPanel = new JPanel();
        controlPanel.setBackground(new Color(30, 30, 30));
        controlPanel.add(startButton);
        controlPanel.add(pauseButton);
        controlPanel.add(resetButton);
        controlPanel.add(Box.createHorizontalStrut(20));
        controlPanel.add(scoreLabel);

        // 添加组件到窗口
        add(gamePanel, BorderLayout.CENTER);
        add(controlPanel, BorderLayout.SOUTH);
    }

    public void updateScore(int newScore) {
        score = newScore;
        scoreLabel.setText("分数: " + score);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                GameMain game = new GameMain();
                game.setVisible(true);
            }
        });
    }
}    